package com.dreamliner.lib.lame.sample;

import android.databinding.DataBindingUtil;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.dreamliner.lib.customdialog.CustomDialogAction;
import com.dreamliner.lib.frame.base.AppManager;
import com.dreamliner.lib.frame.base.BaseCompatActivity;
import com.dreamliner.lib.frame.util.LogUtil;
import com.dreamliner.lib.lame.MP3Recorder;
import com.dreamliner.lib.lame.Mp3RecordListener;
import com.dreamliner.lib.lame.sample.databinding.ActMainBinding;

import java.io.File;
import java.io.IOException;

/**
 * Created by chenzj on 2017/7/4.
 */

public class MainActivity extends BaseCompatActivity {

    private static final String TAG = "MainActivity";

    private ActMainBinding mBinding;

    private MP3Recorder mMP3Recorder;
    private String mRecordFileDirPath = "";
    private String mLastRecordFilePath = "";

    @Override
    protected void initViews(@Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.setContentView(this, R.layout.act_main);
        mBinding.setClick(this);

        mMP3Recorder = new MP3Recorder();
        mMP3Recorder.setOnRecordChangeListener(new Mp3RecordListener() {
            @Override
            public void onVolumeChanged(int value) {
                LogUtil.i(TAG, "onVolumeChanged,value=" + value);
            }

            @Override
            public void onTimeChanged(int value, String localPath) {
                LogUtil.i(TAG, "onTimeChanged,value=" + value + "\tlocalPath=" + localPath);
                mLastRecordFilePath = localPath;
            }

            @Override
            public void onRecordEnd(int length, String localPath) {
                LogUtil.i(TAG, "onRecordEnd,length=" + length);
            }
        });
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_record_bt:
                startRecord();
                mBinding.startRecordBt.setEnabled(false);
                mBinding.playBt.setEnabled(false);
                mBinding.stopRecordBt.setEnabled(true);
                break;
            case R.id.stop_record_bt:
                stopRecord();
                mBinding.startRecordBt.setEnabled(true);
                mBinding.stopRecordBt.setEnabled(false);
                mBinding.playBt.setEnabled(true);
                break;
            case R.id.play_bt:
                play();
                break;
        }
    }

    private void startRecord() {
        mMP3Recorder.startRecording(getVoicePath());
    }

    private void stopRecord() {
        mMP3Recorder.stopRecording();
    }

    private void play() {
        File file = new File(mLastRecordFilePath);
        if (file.exists()) {
            MediaPlayer mediaPlayer = new MediaPlayer();
            try {
                mediaPlayer.setDataSource(mLastRecordFilePath);
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
                showToast(e.getMessage());
            }
        } else {
            showToast("文件不存在");
        }
    }

    private String getVoicePath() {
        if (TextUtils.isEmpty(mRecordFileDirPath)) {
            mRecordFileDirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
                    + AppContext.getInstance().getPackageName() + File.separator;
        }
        return mRecordFileDirPath;
    }


    @Override
    public void onBackPressedSupport() {
        showBaseDialog("是否要退出软件", (dialog, which) -> {
            dialog.dismiss();
            if (which == CustomDialogAction.POSITIVE) {
                AppManager.getInstance().clear();
                System.gc();
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(1);
            }
        });
    }
}
